<script setup lang="ts">
import { NIcon } from 'naive-ui'
import { useAppsStore } from '@/stores/modules/app'
const useStore = useAppsStore()
const { getUserInfo: userInfo } = storeToRefs(useStore)
const { signOut } = useStore

const renderIcon = (icon: string) => {
  return () => {
    return h(NIcon, null, {
      default: () =>
        h('i', {
          class: ['esayIcon', icon],
        }),
    })
  }
}
const options = [
  {
    label: '个人中心',
    key: 'user',
    icon: renderIcon('esayIcon-waixingren'),
  },
  {
    label: '退出登录',
    key: 'logout',
    icon: renderIcon('esayIcon-tuichu'),
  },
]
const active = ref(false)
const activeChange = (val: boolean) => {
  active.value = val
}
const router = useRouter()
const handleSelect = (command: string) => {
  if (command === 'logout') {
    signOut()
    location.reload()
  } else {
    router.push({
      path: '/personalCenter',
    })
  }
}
</script>

<template>
  <div class="user">
    <n-dropdown
      trigger="hover"
      :options="options"
      @updateShow="activeChange"
      @select="handleSelect"
    >
      <span class="avatar-dropdown">
        <n-avatar round :size="30" :src="userInfo.avatar" />
        <div class="user-name">
          <span>{{ userInfo.nickname }}</span>
        </div>
        <i class="esayIcon esayIcon-xiangxia2" :class="{ rotage: active }"></i>
      </span>
    </n-dropdown>
  </div>
</template>

<style scoped lang="scss">
.user {
  margin-left: 10px;
  display: flex;
  align-items: center;
}
.avatar-dropdown {
  display: flex;
  align-content: center;
  align-items: center;
  justify-content: center;
  justify-items: center;
  outline: none;
  color: var(--yang-menu-navtop-text-color);
  .esayIcon {
    transition: all 0.2s;
  }
  .rotage {
    transform: rotate(180deg);
  }
  .user-name {
    position: relative;
    display: flex;
    flex-shrink: 0;
    align-content: center;
    align-items: center;
    height: 40px;
    margin-left: 6px;
    line-height: 40px;
    cursor: pointer;
    span {
      max-width: 100px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
</style>
